<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>在线考试平台</title>
    <link rel="shortcut icon" href="../images/exam.png" type="image/x-icon">
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">

</head>

<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15" style="background: #f7f8fc">
        <div class="layui-col-sm12 layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <table class="layui-hide" id="resource_list" lay-filter="resource_list"></table>

                    <script type="text/html" id="barDemo">

                        {{#  if(d.r_state== '不开放'){ }}
                        <a class="layui-btn layui-btn-xs" lay-event="open">设为开放</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                        {{#  } }}
                        {{#  if(d.r_state== '已开放'){ }}
                        <a class="layui-btn layui-btn-xs" lay-event="close">设为不开放</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                        {{#  } }}
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

</body>
<script type="text/javascript"  src="../lib/layui/layui.js"></script>
<script type="text/javascript" src="../js/xadmin.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script>
    layui.use(['jquery'], function () {
        const $ = layui.jquery;
        get_all_resource();

    })
    function get_all_resource() {
        layui.use(["form", 'jquery', 'layer', 'table'], function () {
            const $ = layui.jquery;
            const layer = layui.layer;
            const table = layui.table;
            table.render({
                elem: '#resource_list'
                , url: 'get_all_resource'
                , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , title: '资源表'
                , cols: [[
                    {field: 'r_id', title: '序号', sort: true}
                    , {field: 'r_name', title: '资源名',edit: "text"}
                    , {field: 'r_src', title: '资源地址'}
                    , {field: 'r_creat_time', title: '上传时间'}
                    , {field: 'r_state', title: '状态', edit: "text"}
                    , {title: '操作', align: 'center', toolbar: '#barDemo'}
                ]]
                , page: {				//分页设置
                    limit: 10,		//默认每一页显示数量
                    limits: [5, 10, 20, 30, 40, 50]		//可选择每页显示的数量
                }
                , parseData: function (res) { //res 即为原始返回的数据
                    var current_pages;
                    //第一次显示的时候this.page=true，把这种情况单独列出
                    if (this.page === true) current_pages = 1;
                    else current_pages = this.page.curr;
                    //根据分页要求选出需要显示的数据
                    let data = res.result.slice(this.limit * (current_pages - 1), this.limit * current_pages);
                    //alert(JSON.stringify(data));
                    return {
                        "code": res.code,
                        "msg": res.message,
                        "count": res.result.length,
                        "data": data
                    }
                }
            });

            //监听行工具事件
            table.on('tool(resource_list)', function (obj) {
                let data = obj.data;
                console.log(obj)
                if (obj.event === 'open') {
                    layer.alert('确定将该资源设为开放？', {icon: 0},
                        function () {
                            $.ajax({
                                type: 'post',
                                url: 'update_resource_state_by_r_id',
                                dataType: 'json',
                                data: {
                                    r_id:data.r_id,
                                    r_state: '已开放',
                                },
                                success: function (data) {
                                    if (data.code === 200) {
                                        layer.msg(data.message, {icon: 6, time: 1000}, function () {
                                            table.reload('resource_list', {})
                                        });
                                    } else {
                                        layer.msg(data.message, {icon: 5, time: 1000});
                                    }
                                }
                            });
                        }
                    );
                } else if (obj.event === 'del') {
                    layer.confirm('确认删除？', {icon: 0}, function () {
                        $.ajax({
                            url: 'del_resource_state_by_r_id',
                            dataType: "json",//数据格式
                            type: "post",//请求方式
                            async: false,//是否异步请求
                            data: {
                                r_id:data.r_id
                            },
                            success: function (data) {
                                if (data.code === 200) {
                                    layer.msg(data.message, {icon: 6, time: 1000}, function () {
                                        table.reload('resource_list', {})
                                    });
                                } else {
                                    layer.msg(data.message, {icon: 5, time: 1000});
                                }
                            },
                            error: function (e) {
                                alert("出现错误！！");
                            }
                        });
                    });
                }else if (obj.event === 'close') {
                    layer.alert('确定将该资源设为不开放？', {icon: 0},
                        function () {
                            $.ajax({
                                type: 'post',
                                url: 'update_resource_state_by_r_id',
                                dataType: 'json',
                                data: {
                                    r_id:data.r_id,
                                    r_state: '不开放',
                                },
                                success: function (data) {
                                    if (data.code === 200) {
                                        layer.msg(data.message, {icon: 6, time: 1000}, function () {
                                            table.reload('resource_list', {})
                                        });
                                    } else {
                                        layer.msg(data.message, {icon: 5, time: 1000});
                                    }
                                }
                            });
                        }
                    );
                }
            });

        });
    }
</script>

</html>